package com.daqsoft.data.project;

import com.daqsoft.dto.StatusEnum;
import com.daqsoft.util.DateUtil;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.*;

/**
 * 流程节点
 * <p>
 * 注意双向链表删除的问题, 需要在mysql中设置删除为SET_NULL
 *
 * @author 周麟东 on 11:55
 */
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Node implements Cloneable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    /**
     * 节点名称
     */
    private String name;

    /**
     * 节点说明
     */
    @Column(name = "node_desc", length = 16777216)
    private String desc;

    /**
     * 该节点状态
     */
    @Enumerated(EnumType.STRING)
    private StatusEnum status = StatusEnum.WAIT;

    /**
     * 所属项目
     */
    @ManyToOne(targetEntity = Project.class)
    private Project project;

    /**
     * 包含任务
     */
    @OneToMany(mappedBy = "node", targetEntity = Task.class)
    private List<Task> tasks;
    /**
     * 节点开始时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date startDate;
    /**
     * 节点结束时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDate;
    /**
     * 节点顺序  从1 - N
     */
    @Column(name = "node_index")
    private int index;

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", this.id);
        map.put("nodeId", this.id);
        map.put("name", this.name);
        map.put("text", this.name);
        map.put("desc", this.desc);
        map.put("status", this.status.name());
        map.put("indexNum", this.index);
        if (this.project != null) {
            map.put("projectId", this.project.getId());
            map.put("projectName", this.project.getProjectName());
            map.put("canEdit", (this.project.getStatus() == StatusEnum.WAIT || this.getProject().getStatus() == StatusEnum.CHANGING)&&this.status != StatusEnum.FINISH);
        } else {
            map.put("projectId", -1);
            map.put("projectName", "#找不到这个节点所属项目#");
            map.put("canEdit", false);
        }
        if (this.startDate != null) {
            map.put("startDate", DateUtil.INS.parse(this.startDate));
        }
        if (this.endDate != null) {
            map.put("endDate", DateUtil.INS.parse(endDate));
        }
        map.put("isTask", false);
//        map.put("iconCls", "x-fa fa-envira");
        map.put("expanded", true);
        map.put("canLeaf", true);
//        map.put("pid", "root");
        if (this.tasks != null && tasks.size() > 0) {
            List<Map<String, Object>> list = new ArrayList<>();
            for (Task task : tasks) {
                list.add(task.toMap());
            }
            map.put("children", list);
        }else {
            map.put("leaf",true);
        }

        return map;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public List<Task> getTasks() {
        return tasks;
    }

    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
    }

    public StatusEnum getStatus() {
        return status;
    }

    public void setStatus(StatusEnum status) {
        this.status = status;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
